package com.hjh.gameforum.controller;

import com.hjh.gameforum.dto.*;
import com.hjh.gameforum.entity.User;
import com.hjh.gameforum.result.PageResult;
import com.hjh.gameforum.result.Result;
import com.hjh.gameforum.service.UserService;
import com.hjh.gameforum.utils.JwtTokenUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
@Tag(name = "用户管理", description = "用户相关操作")  // 替换原来的 @Api
@Slf4j
public class UserController {

    private final UserService userService;  // 使用构造器注入替代@Autowired
    private final JwtTokenUtil jwtTokenUtil;

    @PostMapping("/register")
    @Operation(summary = "用户注册")
    public Result register(@Validated @RequestBody RegisterDto registerDto) {
        log.info("用户注册：{}", registerDto);
        userService.register(registerDto);
        return Result.success();
    }
    @PostMapping("/login")
    @Operation(summary = "用户登录")
    public Result<LoginToken> login(@Validated @RequestBody LoginDto loginDto) {
        log.info("用户登录：{}", loginDto );
        LoginToken loginToken = userService.login(loginDto);
        return Result.success(loginToken);
    }

    @PostMapping("/code")
    @Operation(summary = "发送验证码")
    public Result sendCode(@RequestParam String phone) {
        log.info("发送验证码：{}", phone);
        userService.sendCode(phone);
        return Result.success();
    }

    @PostMapping("/login/phone")
    @Operation(summary = "用户手机号验证码登录")
    public Result<LoginToken> codeLogin(@RequestParam String phone, @RequestParam String code) {
        log.info("用户登录：{}", phone);
        LoginToken loginToken = userService.login(phone, code);
        return Result.success(loginToken);
    }

    @PutMapping
    @Operation(summary = "更新用户信息")
    public Result updateUser(@Validated @RequestBody UserDto userDto) {

        log.info("更新用户信息：{}", userDto);
        userService.updateUser(userDto);
        return Result.success();
    }

    // 获取当前用户信息
    @GetMapping
    @Operation(summary = "获取当前用户信息")
    public Result<UserDto> getCurrentUser() {
        log.info("获取当前用户信息");
        UserDto userDto = userService.getCurrentUser();
        return Result.success(userDto);
    }

    @PutMapping("/status")
    @Operation(summary = "设置用户状态")
    public Result banUser(@RequestParam Long userId, @RequestParam Integer status) {
        log.info("禁用用户：{}", userId);
        userService.setStatus(userId, status);
        return Result.success();
    }

    @PutMapping("/role")
    @Operation(summary = "设置用户权限")
    public Result setRole(@RequestParam Long userId, @RequestParam Integer role) {
        log.info("设置用户角色：{}", userId);
        userService.setRole(userId, role);
        return Result.success();
    }


    @GetMapping("/list")
    @Operation(summary = "分页获取用户列表")
    public Result<PageResult<User>> getUserList(UserQueryParem userQueryParem){
        log.info("获取用户列表");
        PageResult<User> userList = userService.getUserList(userQueryParem);
        return Result.success(userList);
    }

    @GetMapping("/search")
    @Operation(summary = "根据用户名搜索用户")
    public Result<PageResult<User>> searchUser(UserQueryParem userQueryParem) {
        log.info("根据用户名搜索用户：{}", userQueryParem);
        PageResult<User> userList = userService.searchUser(userQueryParem);
        return Result.success(userList);
    }
    @DeleteMapping("/delete")
    @Operation(summary = "删除用户")
    public Result deleteUser(@RequestParam Long userId) {
        log.info("删除用户：{}", userId);
        userService.deleteUser(userId);
        return Result.success();
    }

}